# Copyright (C) 2009 Martin Kirsche

import time
import threading

import wx


class PlayerFrame(wx.Frame):

    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        
        panel = wx.Panel(self, -1)
        
        vSizer = wx.BoxSizer(wx.VERTICAL)
        
        hSizer1 = wx.BoxSizer(wx.HORIZONTAL)
        self.adjustButton = wx.Button(panel, -1, "Adjust", wx.DefaultPosition, wx.DefaultSize, 0)
        hSizer1.Add(self.adjustButton, 0, wx.ALIGN_CENTER|wx.ALL, 5)
        self.urlText = wx.TextCtrl(panel, -1, "lastfm://globaltags/pop", wx.DefaultPosition, [260,-1], 0)
        hSizer1.Add(self.urlText, 0, wx.ALIGN_CENTER|wx.ALL, 5)
        vSizer.Add(hSizer1, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        
        hSizer2 = wx.BoxSizer(wx.HORIZONTAL)
        self.playButton = wx.Button(panel, -1, "Play", wx.DefaultPosition, wx.DefaultSize, 0)
        hSizer2.Add(self.playButton, 0, wx.ALIGN_CENTER|wx.ALL, 5)
        self.skipButton = wx.Button(panel, -1, "Skip", wx.DefaultPosition, wx.DefaultSize, 0)
        hSizer2.Add(self.skipButton, 0, wx.ALIGN_CENTER|wx.ALL, 5)
        self.downloadButton = wx.Button(panel, -1, "Download", wx.DefaultPosition, wx.DefaultSize, 0)
        hSizer2.Add(self.downloadButton, 0, wx.ALIGN_CENTER|wx.ALL, 5)
        vSizer.Add(hSizer2, 0, wx.ALIGN_CENTER|wx.ALL, 5)
        
        hSizer3 = wx.BoxSizer(wx.HORIZONTAL)
        
        self.songImage = wx.StaticText(panel, -1, "No image", wx.DefaultPosition, wx.DefaultSize)
        hSizer3.Add(self.songImage, 0, wx.ALIGN_CENTER|wx.ALL, 5)
        
        gridSizer = wx.GridSizer(0, 2, 0, 0)
        
        artistKey = wx.StaticText(panel, -1, "Artist", wx.DefaultPosition, wx.DefaultSize, 0)
        artistKey.SetForegroundColour(wx.LIGHT_GREY)
        gridSizer.Add(artistKey, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        self.artistValue = wx.StaticText(panel, -1, "text", wx.DefaultPosition, wx.DefaultSize, 0)
        gridSizer.Add(self.artistValue, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        
        albumKey = wx.StaticText(panel, -1, "Album", wx.DefaultPosition, wx.DefaultSize, 0)
        albumKey.SetForegroundColour(wx.LIGHT_GREY)
        gridSizer.Add(albumKey, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        self.albumValue = wx.StaticText(panel, -1, "text", wx.DefaultPosition, wx.DefaultSize, 0)
        gridSizer.Add(self.albumValue, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        
        songKey = wx.StaticText(panel, -1, "Song", wx.DefaultPosition, wx.DefaultSize, 0)
        songKey.SetForegroundColour(wx.LIGHT_GREY)
        gridSizer.Add(songKey, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        self.songValue = wx.StaticText(panel, -1, "text", wx.DefaultPosition, wx.DefaultSize, 0)
        gridSizer.Add(self.songValue, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        
        hSizer3.Add(gridSizer, 0, wx.ALIGN_CENTER|wx.ALL, 5)
        vSizer.Add(hSizer3, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        
        self.gauge = wx.Gauge(panel, -1, 100, wx.DefaultPosition, [100,-1], 0)
        vSizer.Add(self.gauge, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        
        border = wx.BoxSizer()
        border.Add(vSizer, 0, wx.ALL, 15)
        panel.SetSizerAndFit(border)
        self.Fit()


class PlayerApp(wx.App):
                 
    def OnInit(self):
        self.player = None
        self.frame = PlayerFrame(None, -1, "Player")
        self.frame.adjustButton.Bind(wx.EVT_BUTTON, self.adjustClick)
        self.frame.skipButton.Bind(wx.EVT_BUTTON, self.skipClick)
        self.frame.playButton.Bind(wx.EVT_BUTTON, self.playClick)
        self.frame.downloadButton.Bind(wx.EVT_BUTTON, self.downloadClick)
        
        self.frame.Show(True)
        self.SetTopWindow(self.frame)
        threading.Thread(target=self.poll).start()
        return True
    
    def adjustClick(self, event):
        if not self.player: return
        self.player.adjust(self.frame.urlText.Value)
        
    def skipClick(self, event):
        if not self.player: return
        self.player.skip()
    
    def playClick(self, event):
        if not self.player: return
        if self.player.isPaused:
            self.player.play()
        else:
            self.player.pause()
    
    def downloadClick(self, event):
        self.player.download()
    
    def poll(self):
        while True: 
            if self.player:
                time.sleep(self.player.poll()/1000.0)
            else:
                time.sleep(1)
        
if __name__ == '__main__':
    PlayerApp(0).MainLoop()